//
//  NSDate+JTExtension.m
//  百思不得姐
//
//  Created by Robin Zhang on 2017/1/12.
//  Copyright © 2017年 BeiJingWangDianJieTong. All rights reserved.
//

#import "NSDate+JTExtension.h"

@implementation NSDate (JTExtension)

- (BOOL)jt_isThisYear {
    
    // 判断self这个日期是否为今年
    NSCalendar *calendar = [NSCalendar jt_calendar];
    
    // 年
    NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    
    return (selfYear == nowYear);
}

//- (BOOL)jt_isThisYear {
//    
//    // 判断self这个日期是否为今年
//    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//    [fmt setDateFormat:@"yyyy"];
//    
//    // 年
//    NSString *selfYear = [fmt stringFromDate:self];
//    NSString *nowYear = [fmt stringFromDate:[NSDate date]];
//    
//    return [selfYear isEqualToString:nowYear];
//}

- (BOOL)jt_isToday {
    
    // 判断self这个日期是否为今天
    NSCalendar *calendar = [NSCalendar jt_calendar];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    return (selfCmps.year == nowCmps.year)
        && (selfCmps.month == nowCmps.month)
        && (selfCmps.day == nowCmps.day);
}

//- (BOOL)jt_isToday {
//    
//    // 判断self这个日期是否为今天
//    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//    [fmt setDateFormat:@"yyyyMMdd"];
//
//    // 年-月-日
//    NSString *selfDay = [fmt stringFromDate:self];
//    NSString *nowDay = [fmt stringFromDate:[NSDate date]];
//
//    return [selfDay isEqualToString:nowDay];
//}

- (BOOL)jt_isYesterday {
    
    // 判断self这个日期是否为昨天
    
    // self -> 2017-01-11 08:11:44 -> 2017-01-11 00:00:00
    // now  -> 2017-01-12 21:28:09 -> 2017-01-12 00:00:00
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyyMMdd"];
    
    NSString *selfString = [fmt stringFromDate:self]; // 20170111
    NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20170112
    
    NSDate *selfDate = [fmt dateFromString:selfString]; // 2017-01-11 00:00:00
    NSDate *nowDate = [fmt dateFromString:nowString]; // 2017-01-12 00:00:00
    
    NSCalendar *calendar = [NSCalendar jt_calendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
    
    return (cmps.year == 0)
        && (cmps.month == 0)
        && (cmps.day == 1);
}

- (BOOL)jt_isTomorrow {
    
    // 判断self是否是明天
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyyMMdd"];
    
    NSString *selfString = [fmt stringFromDate:self]; // 20170111
    NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20170112
    
    NSDate *selfDate = [fmt dateFromString:selfString]; // 2017-01-11 00:00:00
    NSDate *nowDate = [fmt dateFromString:nowString]; // 2017-01-12 00:00:00
    
    NSCalendar *calendar = [NSCalendar jt_calendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
    
    return (cmps.year == 0)
    && (cmps.month == 0)
    && (cmps.day == -1); /** 昨天明天区别在于
                          cmps.day == 1(昨天)
                                     -1(明天)   
                          */
}

@end
